﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using yyylib;

namespace YYY
{
    /// <summary>
    /// 启动进程、关闭进程操作
    /// </summary>
    public class OperProcess
    {
        #region init update env
        public static void ExitProgame(string appName)
        {
            if (IfExist(appName))
            {
                CloseExe(appName);
            }
        }
        #endregion init update env

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dllName">dll 路径,不要.dll后缀</param>
        /// <param name="appName">APP 全名,包名+类名</param>
        public static bool StartFromDll(string dllName,string appName) {
            Assembly ass = Assembly.LoadFrom(dllName+".dll");
            Type type = ass.GetType(appName);
            if (null != type)
            {
                IYYRunable obj = (IYYRunable)Activator.CreateInstance(type);
                if (obj!=null) {
                    obj.ExitOnClose(true);
                    obj.Open();
                    return true;
                }
            }
            return false;
        }

        private static void Obj_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        //#region updated start process
        //public void StartProcess(string appName)
        //{
        //    string path = System.Environment.CurrentDirectory;
        //    if (!IfExist(appName))
        //    {
        //        StartExe(path, appName+".exe");
        //    }
        //    CloseExe(ConstFile.ThisName);
        //}

        //#endregion

        //#region updated start process
        //public void StartUpdateProcess()
        //{
        //    string path = System.Environment.CurrentDirectory;
        //    if (!IfExist(ConstFile.ThisName))
        //    {
        //        StartExe(path, ConstFile.ThisName+ ".exe");
        //    }
        //    CloseExe(ConstFile.MainExeName);
        //}

        //#endregion


        #region 启动进程、关闭进程、判断进程是否存在
        public static void StartExe(string fileName)
        {
            StartExe(System.Environment.CurrentDirectory, fileName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workDir">启动exe绝对路径</param>
        /// <param name="fileName">启动exe 名词,不含 .exe</param>
        public static void StartExe(string workDir,string fileName)
        {
            Process proc = new Process();
            proc.StartInfo.UseShellExecute = true;//是否使用操作系统外壳程序启动进程

            proc.StartInfo.WorkingDirectory = workDir;//启动进程的初始目录
            proc.StartInfo.FileName = fileName;
            proc.Start();
        }


        //exeName 关闭的exe进程名
        private static void CloseExe(string exeName)
        {
            Process[] arrPro = Process.GetProcessesByName(exeName);
            foreach (Process pro in arrPro)
                pro.Kill();
        }
        //processName 进程名
        private static bool IfExist(string processName)
        {
            Process[] pro = Process.GetProcessesByName(processName);
            return pro.Count() > 0;
        }
        #endregion 启动进程、关闭进程
    }
}
